{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a5d56268",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import os, time, datetime, ast, json\n",
    "\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline\n",
    "\n",
    "import geopandas as gpd\n",
    "import geopy.distance as geodist\n",
    "\n",
    "os.chdir(\"/Users/xiaosongw/Dropbox/Research/InformedSources/Replication/Build\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b93aebee",
   "metadata": {},
   "source": [
    "# load traffic zones"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d8b24315",
   "metadata": {},
   "outputs": [],
   "source": [
    "gdf_tzn=gpd.read_file(\"./Input/VITM2975\")\n",
    "print(gdf_tzn.crs)\n",
    "gdf_tzn.columns=gdf_tzn.columns.str.lower()\n",
    "gdf_tzn['tzn']=gdf_tzn['tzn'].astype(int)\n",
    "gdf_tzn['centroid']=gdf_tzn['geometry'].centroid\n",
    "gdf_tzn['coor']=gdf_tzn['centroid'].apply(lambda x: '{:.8f},{:.8f}'.format(x.x, x.y))\n",
    "gdf_tzn['lat']=gdf_tzn['centroid'].y\n",
    "gdf_tzn['lon']=gdf_tzn['centroid'].x\n",
    "print(gdf_tzn['tzn'].nunique())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8889a1f7",
   "metadata": {},
   "outputs": [],
   "source": [
    "d_tzn2coor=gdf_tzn.set_index('tzn')['coor'].to_dict()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1a8a1a54",
   "metadata": {},
   "source": [
    "# traffic zones in Melbourne "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0565ed53",
   "metadata": {},
   "outputs": [],
   "source": [
    "gdf0_ucl = gpd.read_file(\"./Input/1270055004_ucl_2016_aust_shape\")\n",
    "gdf_tzn['mel'] = (gdf_tzn['centroid'].within(\n",
    "    gdf0_ucl.loc[gdf0_ucl['UCL_NAME16']=='Melbourne', 'geometry'].values[0])).astype(int)\n",
    "\n",
    "l_tzn_mel = gdf_tzn.loc[gdf_tzn['mel']==1, 'tzn'].unique()\n",
    "print('{} traffic zones in melbourne'.format(len(l_tzn_mel)))\n",
    "print('{} traffic zones not in melbourne'.format((1-gdf_tzn['mel']).sum()))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b6ba23f7-190c-4827-8119-95286b38b5ab",
   "metadata": {},
   "outputs": [],
   "source": [
    "d_tzn2coor_mel = {}\n",
    "for i in d_tzn2coor.keys():\n",
    "    if i in l_tzn_mel:\n",
    "        d_tzn2coor_mel[i] = d_tzn2coor[i]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3ce9566c-4695-40a9-a118-a64fa9b1309c",
   "metadata": {},
   "outputs": [],
   "source": [
    "with open(\"./Output/tzn2coor_mel.txt\", \"w\") as text_file:\n",
    "    text_file.write(json.dumps(d_tzn2coor_mel))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3e1060bb",
   "metadata": {},
   "source": [
    "# OD traffic "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "daff8172",
   "metadata": {},
   "outputs": [],
   "source": [
    "df_t1=pd.read_csv(\n",
    "    \"./Input/Y2018_RC22v1_06_CAR_DEMAND_AM.csv\")\n",
    "print(df_t1.shape)\n",
    "df_t2=pd.read_csv(\n",
    "    \"./Input/Y2018_RC22v1_06_CAR_DEMAND_IP.csv\")\n",
    "print(df_t2.shape)\n",
    "df_t3=pd.read_csv(\n",
    "    \"./Input/Y2018_RC22v1_06_CAR_DEMAND_PM.csv\")\n",
    "print(df_t3.shape)\n",
    "df_t4=pd.read_csv(\n",
    "    \"./Input/Y2018_RC22v1_06_CAR_DEMAND_OP.csv\")\n",
    "print(df_t4.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e5b330b8",
   "metadata": {},
   "outputs": [],
   "source": [
    "df_od=df_t1.rename(columns={'Demand':'t1'}).merge(\n",
    "    df_t2.rename(columns={'Demand':'t2'}), on=['From', 'To'], how='outer').merge(\n",
    "    df_t3.rename(columns={'Demand':'t3'}), on=['From', 'To'], how='outer').merge(\n",
    "    df_t3.rename(columns={'Demand':'t4'}), on=['From', 'To'], how='outer')\n",
    "df_od['n']=df_od[['t1', 't2', 't3', 't4']].sum(axis=1)\n",
    "display(df_od.head(2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e39cb600",
   "metadata": {},
   "outputs": [],
   "source": [
    "df_od_out = df_od.rename(columns={'From':'tzn0', 'To':'tzn1'}).groupby(\n",
    "    ['tzn0', 'tzn1'])['n'].sum().reset_index()\n",
    "print(df_od_out.shape)\n",
    "df_od_out = df_od_out[df_od_out['n']>1].sort_values(['tzn0', 'tzn1'], ignore_index=True).copy()\n",
    "print(df_od_out.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ed8bddcc-0e9e-4a7a-9885-df32b207b737",
   "metadata": {},
   "outputs": [],
   "source": [
    "df_od_mel = df_od_out[(df_od_out['tzn0'].isin(l_tzn_mel))&(df_od_out['tzn1'].isin(l_tzn_mel))].copy()\n",
    "print(df_od_mel.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f6cf0d64-854b-4697-aa3d-e555fc20ec15",
   "metadata": {},
   "outputs": [],
   "source": [
    "df_od_out.to_csv(\"./Output/od_long_vic.csv\", index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4059bd27",
   "metadata": {},
   "outputs": [],
   "source": [
    "df_od_mel.to_csv(\"./Output/od_long_mel.csv\", index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "18677bc6",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ac963d4a",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "hide_input": false,
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.16"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
